<?php
// +----------------------------------------------------------------------
// | Ghome/Home/Controller/ZCEController
// +----------------------------------------------------------------------
// | Author: alex <gt199899@gmail.com>
// +----------------------------------------------------------------------
// | Version: 2014-04-08
// +----------------------------------------------------------------------
// | Copyright: Perfect Is Shit
// +----------------------------------------------------------------------
namespace Home\Controller;
class ZCEController extends InitController{
	
/* 	// 大纲
	protected $examInformation = array(
			'PHP Basics'					=>	array(
					'Syntax'							=>	array(
							'PHP tags',
							'Escaping from HTML',
							'Instruction separation',
							'Comments',
					),
					'Types'								=>	array(
							'Introduction',
							'Booleans',
							'Integers',
							'Floating point numbers',
							'Strings',
							'Arrays',
							'Objects',
							'Resources',
							'NULL',
							'Callbacks',
							'Pseudo-types and variables used in this documentation',
							'Type Juggling',
					),
					'Operators'							=>	array(
							'Operator Precedence',
							'Arithmetic Operators',
							'Assignment Operators',
							'Bitwise Operators',
							'Comparison Operators',
							'Error Control Operators',
							'Execution Operators',
							'Incrementing/Decrementing Operators',
							'Logical Operators',
							'String Operators',
							'Array Operators',
							'Type Operators',
					),
					'Variables'							=>	array(
							'Basics',
							'Predefined Variables',
							'Variable scope',
							'Variable variables',
							'Variables From External Sources',
					),
					'Control Structures'				=>	array(
							'introduction',
							'if',
							'else',
							'elseif & else if',
							'Alternative syntax for control structures',
							'while',
							'dowhile',
							'for',
							'foreach',
							'break',
							'continue',
							'switch',
							'declare',
							'return',
							'require',
							'include',
							'requireonce',
							'includeonce',
							'goto',
					),
					'Language Constructs and Functions'	=>	array(
					),
					'Namespaces'						=>	array(
					),
					'Extensions'						=>	array(
					),
					'Config'							=>	array(
					),
					'Performance & byte code caching'	=>	array(
					),
			),
			'Data Format & Types'			=>	array(
					'XML Basics'						=>	array(
					),
					'Simple XML'						=>	array(
					),
					'XML Extension'						=>	array(
					),
					'WebService Basics'					=>	array(
					),
					'SOAP'								=>	array(
					),
					'JSON'								=>	array(
					),
					'DataTime'							=>	array(
					),
					'DOM Document'						=>	array(
					),
			),
			'Object Oriented Programming'	=>	array(
					'Instantiation'						=>	array(
					),
					'Modifiers & Inheritance'			=>	array(
					),
					'Interfaces'						=>	array(
					),
					'Exceptions'						=>	array(
					),
					'Autoload'							=>	array(
					),
					'Reflection'						=>	array(
					),
					'Type Hinting'						=>	array(
					),
					'Class Constants'					=>	array(
					),
					'Late Static Binding'				=>	array(
					),
					'Magic Methods'					=>	array(
					),
					'Instance Methods & Properties'		=>	array(
					),
					'SPL'								=>	array(
					),
					'Traits'							=>	array(
					),
			),
			'Functions'						=>	array(
					'Arguments'							=>	array(
					),
					'Variables'							=>	array(
					),
					'References'						=>	array(
					),
					'Returns'							=>	array(
					),
					'Variable Scope'					=>	array(
					),
					'Anonymous Functions'				=>	array(
					),
					'closures'							=>	array(
					),
			),
			'Web Features'					=>	array(
					'Sessions'							=>	array(
					),
					'Forms'								=>	array(
					),
					'Get and Post data'					=>	array(
					),
					'Cookies'							=>	array(
					),
					'HTTP headers'						=>	array(
					),
					'HTTP Authentication'				=>	array(
					),
					'HTTP Status Codes'					=>	array(
					),
			),
			'I&O'							=>	array(
					'Files'								=>	array(
					),
					'Reading'							=>	array(
					),
					'Writing'							=>	array(
					),
					'File System Functions'				=>	array(
					),
					'Streams'							=>	array(
					),
					'Contexts'							=>	array(
					),
			),
			'Security'						=>	array(
					'Configuration'						=>	array(
					),
					'Session Security'					=>	array(
					),
					'CrossSite Scripting'				=>	array(
					),
					'CrossSite Request Forgeries'		=>	array(
					),
					'SQL Injection'						=>	array(
					),
					'Filter Input'						=>	array(
					),
					'Escape Output'						=>	array(
					),
					'Encryption,Hashing algorithms'		=>	array(
					),
					'File uploads'						=>	array(
					),
					'PHP Configuration'					=>	array(
					),
					'Password hashing API'				=>	array(
					),
			),
			'Strings & Patterns'			=>	array(
					'Quoting'							=>	array(
					),
					'Matching'							=>	array(
					),
					'Extracting'						=>	array(
					),
					'Searching'							=>	array(
					),
					'Replacing'							=>	array(
					),
					'Formatting'						=>	array(
					),
					'PCRE'								=>	array(
					),
					'Encodings'							=>	array(
					),
			),
			'Databases & SQL'				=>	array(
					'SQL'								=>	array(
					),
					'Joins'								=>	array(
					),
					'Prepared Statements'				=>	array(
					),
					'Transactions'						=>	array(
					),
					'PDO'								=>	array(
					),
			),
			'Arrays'						=>	array(
					'Associative Arrays'				=>	array(
					),
					'Array lteration'					=>	array(
					),
					'Array Functions'					=>	array(
					),
					'SPL Object as arrays'				=>	array(
					),
					'Casting'							=>	array(
					),
			),
	); */
	
	// 文本信息路径
	public $zceTextPath;
	
	/**
	 * 初始化
	 * 定义文本信息路径
	 * @access public
	 * @see \Home\Controller\EntranceController::_initialize()
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	public function _initialize(){
		parent::_initialize();
		$this->zceTextPath = C('PHY_PATH.PUBLIC') . "/ZCE";
	}
	
	/**
	 * zce展示默认页面
	 * 展示zce大纲
	 * @access public
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	public function index(){
		$outline = $this->zceOutline();
		$this->assign('examinfo', $outline);
		$this->display();
	}
	
	/**
	 * 单条目展示和单条目编辑的GET参数处理
	 * @access public
	 * @return array['l1'] zce大纲一级条目
	 * @return array['l2'] zce大纲二级条目
	 * @return array['l3'] zce大纲三级条目
	 * @return array['entrynavi'] 当前位置导航信息
	 * @return array['goalfile'] 当前条目需要加载的内容目标文件
	 * @return array['nonefile'] 当前条目是否拥有内容目标文件标示，TRUE没有文件，FALSE有文件
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	private function dealGetParams(){
		$param = I('get.p');
		$outline = $this->zceOutline();
		$level1 = '';
		$level2 = '';
		$level3 = '';
		foreach($outline as $key=>$value){
			foreach($value as $k=>$v){
				foreach($v as $m=>$n){
					if($param === md5($key.$k.$n)){
						$level1 = $key;
						$level2 = $k;
						$level3 = $n;
					}
					else{
						continue;
					}
				}
			}
		}
		
		$this->assign('level1', $level1);
		$this->assign('level2', $level2);
		$this->assign('level3', $level3);
		$this->assign('infourl', U('Home/ZCE/info', array('p'=>md5($level1.$level2.$level3))));
		$this->assign('editurl', U('Home/ZCE/edit', array('p'=>md5($level1.$level2.$level3))));
		
		$goal_file = $this->zceTextPath . "/" . $level1 . "/" . $level2 . "/" . $level3 . ".inc";
		$none_file = false;
		if(!file_exists($goal_file)){
			$none_file = true;
		}
		$this->assign('nonerecord', $none_file);
		
		return array(
				'l1'		=>	$level1, 
				'l2'		=>	$level2, 
				'l3'		=>	$level3,
				'goalfile'	=>	$goal_file,
				'nonefile'	=>	$none_file,
		);
	}
	
	/**
	 * 获取zce大纲信息
	 * @access private
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	private function zceOutline(){
		$outline = file_get_contents($this->zceTextPath . '/outline.inc');
		$outline = json_decode($outline, true);
		return $outline;
	}
	
	/**
	 * zce条目信息展示
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	public function info(){
		$params_data = $this->dealGetParams();
		$content = file_get_contents($params_data['goalfile']);
		if(empty($content)){
			$this->assign('nonerecord', true);
		}
		$this->assign('content', $content);
		$this->display();
	}
	
	/**
	 * zce条目信息编辑
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	public function edit(){
		$params_data = $this->dealGetParams();
		if($params_data['nonefile'] === true){
			$content = '';
		}
		else{
			$content = file_get_contents($params_data['goalfile']);
		}
		$this->assign('content', $content);
		$this->assign('file', base64_encode($params_data['l1'] . "/" . $params_data['l2'] . "/" . $params_data['l3']));
		$this->display();
	}
	
	/**
	 * zce条目信息保存
	 * @author alex
	 * @version 20140410
	 * @copyright Perfect Is Shit
	 */
	public function editSave(){
		$result = $this->eventClass->editSave();
		if($result){
			$this->ajaxReturn(true);
		}
		else{
			$this->ajaxReturn(false);
		}
	}
	
}